package com.baibei.product.merge.detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.model.AddressInfo;
import com.baibei.model.MergeOrderInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.module.GlideApp;
import com.baibei.product.R;
import com.baibei.product.R2;
import com.baibei.ui.AppUI;
import com.rae.swift.Rx;
import com.rae.widget.dialog.DialogBuilder;
import com.rae.widget.dialog.IAppDialog;
import com.rae.widget.dialog.IAppDialogClickListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 拼单详情
 * Created by ChenRui on 2017/8/15 0015 17:01.
 */
@Route(path = AppRouter.ROUTER_MERGE_ORDER_DETAIL)
public class MergeOrderDetailActivity extends BasicActivity implements MergeOrderDetailContract.View {

    @BindView(R2.id.img_product)
    ImageView mImgProduct;
    @BindView(R2.id.tv_title)
    TextView mTitleView;
    @BindView(R2.id.tv_price)
    TextView mPriceView;
    @BindView(R2.id.progress)
    ProgressBar mProgressBar;
    @BindView(R2.id.tv_progress)
    TextView mProgressText;
    @BindView(R2.id.tv_total_count)
    TextView mTotalCount;
    @BindView(R2.id.tv_count)
    TextView mCount;
    @BindView(R2.id.tv_mine_count)
    TextView mMineCount;
    @BindView(R2.id.tv_address)
    TextView mAddress;
    private MergeOrderInfo mMergeOrderInfo;
    private MergeOrderDetailContract.Presenter mPresenter;

    private IAppDialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merge_order_detail);
        mPresenter = inject(MergeOrderDetailContract.Presenter.class);
        mDialog = new DialogBuilder(this)
                .setMessage("抱歉，该功能还在开发中，我们客服人员会线下与您联系，以便了解您的需求")
                .positiveButtonClickListener(new IAppDialogClickListener() {
                    @Override
                    public void onClick(IAppDialog iAppDialog, int i) {
                        iAppDialog.dismiss();
                        AppUI.loading(getContext(), "正在拼单，请稍后");
                        mPresenter.merge();
                    }
                })
                .positiveButtonText("我知道了")
                .build();

        mMergeOrderInfo = getIntent().getParcelableExtra("mergeOrderInfo");
        if (mMergeOrderInfo != null) {
            onLoadOrderInfo(mMergeOrderInfo);
        }

        mPresenter.start();
    }

    private void onLoadOrderInfo(MergeOrderInfo m) {
        GlideApp.with(this).load(m.getProductPic()).into(mImgProduct);
        mTitleView.setText(m.getName());
        mProgressBar.setProgress((int) (m.getProgress() * 100));
        mProgressText.setText(Rx.formatPercent(m.getProgress()));
        mPriceView.setText(getString(R.string.format_price, Rx.formatPrice(m.getCrawdPrice())));
        mTotalCount.setText(m.getRemainCountStr());
        mCount.setText(m.getCrawdedCount());
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }

    @OnClick(R2.id.btn_done)
    public void onDoneClick() {
        if (mPresenter.checkParams()) {
            mDialog.show();
        }
    }

    @OnClick(R2.id.ll_address)
    public void onAddressLayoutClick() {
        AppRouter.routeToOrderAddress(this, true);
    }

    @OnClick(R2.id.img_back)
    public void onBackClick() {
        finish();
    }

    @Override
    protected void onActivityResultOK(int requestCode, Intent data) {
        super.onActivityResultOK(requestCode, data);
        if (requestCode == AppRouter.REQ_ADDRESS && data != null) {
            AddressInfo addressInfo = data.getParcelableExtra("data");
            this.mAddress.setText(addressInfo.getFullAddress());
        }
    }

    @Override
    public MergeOrderInfo getMergeOrderInfo() {
        return mMergeOrderInfo;
    }

    @Override
    public int getQuantity() {
        return Rx.parseInt(mMineCount.getText().toString());
    }

    @Override
    public String getAddress() {
        return mAddress.getText().toString();
    }


    @Override
    public void onMergeFailed(String msg) {
        AppUI.dismiss();
        AppUI.failed(this, msg);
    }

    @Override
    public void onMergeSuccess() {
        AppUI.dismiss();
        AppUI.success(this, "拼单成功");
    }

    @Override
    public void onLoadAddress(AddressInfo addressInfo) {
        mAddress.setText(addressInfo.getRegion() + " " + addressInfo.getAddress());
    }
}
